package cn.darkhorse.mesadmin.controller.device;


import cn.darkhorse.common.constant.CommonConstant;
import cn.darkhorse.device.dto.MesRepairInfoDTO;
import cn.darkhorse.device.pojo.MesDeviceType;
import cn.darkhorse.device.pojo.MesRepairInfo;
import cn.darkhorse.device.service.MesDeviceTypeService;
import cn.darkhorse.device.service.MesRepairInfoService;
import cn.darkhorse.device.vo.MesRepairInfoVO;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.mapper.SysDeptMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 维修单 前端控制器
 * </p>
 *
 * @author maoguanjun
 * @since 2022-11-11
 */
@RestController
@RequestMapping("/device/mesRepairInfo")
public class MesRepairInfoController extends BaseController {
   @Resource
   private MesRepairInfoService mesRepairInfoService;
   @Resource
   private SysDeptMapper sysDeptMapper;
   @Resource
   private SysUserMapper sysUserMapper;
   @Resource
   private MesDeviceTypeService deviceTypeService;
    /**
     * 列表
     * @param mesRepairInfoDTO
     * @return
     */

    @RequestMapping(value = "/list",method = RequestMethod.POST)
    public TableDataInfo list( MesRepairInfoDTO mesRepairInfoDTO){
        startPage();
        List<MesRepairInfoVO> mesRepairInfoVOS = mesRepairInfoService.listAll(mesRepairInfoDTO);
        return  getDataTable(mesRepairInfoVOS);
    }

    /**
     * 导出
     * @param mesRepairInfoDTO
     * @return
     */
    @RequestMapping(value = "/export",method = RequestMethod.POST)
    public AjaxResult export( MesRepairInfoDTO mesRepairInfoDTO){
        List<MesRepairInfoVO> mesRepairInfoVOS = mesRepairInfoService.listAll(mesRepairInfoDTO);
        List<SysDept> sysDepts = sysDeptMapper.selectDeptListNoAuth(new SysDept());
        Map<Long, String> deptMap = sysDepts.stream().collect(Collectors.toMap(SysDept::getDeptId,SysDept::getDeptName));
        List<SysUser> sysUsers = sysUserMapper.selectUserListNoAuth(new SysUser());
        Map<Long, String> userMap = sysUsers.stream().collect(Collectors.toMap(SysUser::getUserId,SysUser::getNickName));
        List<MesDeviceType> deviceTypes=deviceTypeService.list();
        Map<Long, String> deviceTypeMap = deviceTypes.stream().collect(Collectors.toMap(MesDeviceType::getId,MesDeviceType::getTypeName));
        mesRepairInfoVOS.forEach(item->{
            if(null!=item.getRepairer()&&userMap.containsKey(item.getRepairer())){
                item.setRepaierName(userMap.get(item.getRepairer()));
            }
            if(null!=item.getReceiver()&&userMap.containsKey(item.getReceiver())){
                item.setReceiverName(userMap.get(item.getReceiver()));
            }
            if(null!=item.getApprover()&&userMap.containsKey(item.getApprover())){
                item.setApproverName(userMap.get(item.getApprover()));
            }
            if(null!=item.getHeader()&&userMap.containsKey(item.getHeader())){
                item.setHeaderName(userMap.get(item.getHeader()));
            }
            if(null!=item.getReceiveCreator()&&userMap.containsKey(item.getReceiveCreator())){
                item.setReceiverCreatorName(userMap.get(item.getReceiveCreator()));
            }
            //TODO 10.17
            StringBuilder assignedPersonNames= new StringBuilder();
            StringBuilder maintenancePersonNames= new StringBuilder();
            String assignedPersons = item.getAssignedPersons();
            String maintenancePersons = item.getMaintenancePersons();
            if(StringUtils.isNotBlank(assignedPersons)){
                String[] assigneds = assignedPersons.split(",");
                for(String ass:assigneds){
                    String nickName = userMap.get(Long.valueOf(ass));
                    if(StringUtils.isNotBlank(nickName)){
                        assignedPersonNames.append(nickName);
                        assignedPersonNames.append(",");
                    }
                }
            }
            if(StringUtils.isNotBlank(maintenancePersons)){
                String[] maintenances = maintenancePersons.split(",");
                for(String mai:maintenances){
                    String nickName = userMap.get(Long.valueOf(mai));
                    if(StringUtils.isNotBlank(nickName)){
                        maintenancePersonNames.append(nickName);
                        maintenancePersonNames.append(",");
                    }
                }
            }
            
            if(null!=item.getDept()&&deptMap.containsKey(item.getDept())){
                item.setDeptName(deptMap.get(item.getDept()));
            }
            if(null!=item.getReviceCreatorDept()&&deptMap.containsKey(item.getReviceCreatorDept())){
                item.setReviceCreatorDeptName(deptMap.get(item.getReviceCreatorDept()));
            }
            if(null!=item.getDeviceType()&&deviceTypeMap.containsKey(item.getDeviceType())){
                item.setDeviceTypeName(deviceTypeMap.get(item.getDeviceType()));
            }
        });
        ExcelUtil<MesRepairInfoVO> util = new ExcelUtil<>(MesRepairInfoVO.class);
        return  util.exportExcel(mesRepairInfoVOS,"设备维修管理");
    }

    /**
     * 查看
     * @param id
     * @return
     */
    @RequestMapping(value = "/view",method = RequestMethod.POST)
    public AjaxResult view(Long id){
        if(null==id){
            return  AjaxResult.error("缺失参数！");
        }
        MesRepairInfoVO mesRepairInfoVO = mesRepairInfoService.view(id);
        return AjaxResult.success(mesRepairInfoVO);
    }

    /**
     * 保存
     * @param mesRepairInfo
     * @return
     */
    @RequestMapping(value = "/save",method = RequestMethod.POST)
    public AjaxResult save(@Validated @RequestBody MesRepairInfo mesRepairInfo){
        if(null==mesRepairInfo){
            return  AjaxResult.error("缺失参数！");
        }
        return AjaxResult.success(mesRepairInfoService.saveMesRepairInfo(mesRepairInfo));
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    public AjaxResult delete(Long id){
        if(null==id){
            return AjaxResult.error("缺失参数！");
        }
        boolean result= mesRepairInfoService.delete(id);
        return AjaxResult.success(result);
    }

    /**
     * 派单
     * @param id
     * @return
     */
   /* @RequestMapping(value = "/dispatch",method = RequestMethod.POST)
    public AjaxResult dispatch(Long id,Long userId){
        if(null==userId||null==id){
            return AjaxResult.error("缺失参数！");
        }
        boolean result= mesRepairInfoService.dispatch(id,userId);
        return AjaxResult.success(result);
    }*/
    //TODO 10.17 派单多选人员
    @RequestMapping(value = "/dispatch",method = RequestMethod.POST)
    public AjaxResult dispatch(Long id,String userIds){
        if(null==userIds||null==id){
            return AjaxResult.error("缺失参数！");
        }
        boolean result= mesRepairInfoService.dispatch2(id,userIds);
        return AjaxResult.success(result);
    }

    /**
     * 取消派单
     * @param id
     * @return
     */
    @RequestMapping(value = "/cancel-dispatch",method = RequestMethod.POST)
    public AjaxResult cancelDispatch(Long id){
        if(null==id){
            return AjaxResult.error("缺失参数！");
        }
        boolean result= mesRepairInfoService.cancelDispatch(id);
        return AjaxResult.success(result);
    }

    /**
     * 接单
     * @param id
     * @return
     */
    @RequestMapping(value = "/receive",method = RequestMethod.POST)
    public AjaxResult receive(Long id,Long receiver){
        if(null==receiver||null==id){
            return AjaxResult.error("缺失参数！");
        }
        boolean result= mesRepairInfoService.receive(id,receiver);
        return AjaxResult.success(result);
    }
    /**
     * 登记
     * @param mesRepairInfoDTO
     * @return
     */
    @RequestMapping(value = "/registration",method = RequestMethod.POST)
    public AjaxResult registration(@RequestBody MesRepairInfoDTO mesRepairInfoDTO){
        if(null==mesRepairInfoDTO.getReceiveCreator()||null==mesRepairInfoDTO.getId()){
            return AjaxResult.error("缺失参数！");
        }
        if(CommonConstant.YES.equals(mesRepairInfoDTO.getApplyStatus())&&mesRepairInfoDTO.getMesSpareApplyRecordsList().isEmpty()){
            return AjaxResult.error("未选择备件！");
        }
        boolean result= mesRepairInfoService.registration(mesRepairInfoDTO);
        return AjaxResult.success(result);
    }
    /**
     * 评价
     * @param id
     * @return
     */
    @RequestMapping(value = "/evaluate",method = RequestMethod.POST)
    public AjaxResult evaluate(Long id,Integer level,String content,Long userId){
        if(null==id){
            return AjaxResult.error("缺失参数！");
        }
        boolean result= mesRepairInfoService.evaluate(id,level,content,userId);
        return AjaxResult.success(result);
    }
    /**
     * 提交
     * @param id
     * @return
     */
    @RequestMapping(value = "/submit",method = RequestMethod.POST)
    public AjaxResult submit(Long id){
        if(null==id){
            return AjaxResult.error("缺失参数！");
        }
        boolean result= mesRepairInfoService.submit(id);
        return AjaxResult.success(result);
    }

    /**
     * 审核通过
     * @param id
     * @return
     */
    @RequestMapping(value = "/audit",method = RequestMethod.POST)
    public AjaxResult audit(Long id,Long approver,String approveTime){
        if(null==id){
            return AjaxResult.error("缺失参数！");
        }
        boolean result= mesRepairInfoService.audit(id,approver,approveTime, CommonConstant.AUDIT_STATUS_REVIEWED);
        return AjaxResult.success(result);
    }
    /**
     * 审核不通过
     * @param id
     * @return
     */
    @RequestMapping(value = "/audit-disagree",method = RequestMethod.POST)
    public AjaxResult auditDisagree(Long id,Long approver,String approveTime){
        if(null==id){
            return AjaxResult.error("缺失参数！");
        }
        boolean result= mesRepairInfoService.audit(id,approver,approveTime,CommonConstant.AUDIT_STATUS_PENDING_TRIAL);
        return AjaxResult.success(result);
    }

    /**
     * 弃审
     * @param id
     * @return
     */
    @RequestMapping(value = "/waiver",method = RequestMethod.POST)
    public AjaxResult waiver(Long id){
        if(null==id){
            return AjaxResult.error("缺失参数！");
        }
        boolean result= mesRepairInfoService.waiver(id);
        return AjaxResult.success(result);
    }

}

